package com.mmp.main.servlet.report;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mmp.common.servlet.BasicServlet;
import com.mmp.main.entity.JobOrder;
import com.mmp.main.util.Enums;
import com.mmp.main.util.WebUtil;

@WebServlet(urlPatterns = { "/ViewOpenedJobOrders", "/ViewLateJobOrders" })
public class ViewJobOrdersActive extends BasicServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		int siteId = getSessionSiteId(request);
		int locationId = getSessionLocationId(request);
		int month = WebUtil.getParamValueAsInteger(request, "month", 0);
		int year = WebUtil.getParamValueAsInteger(request, "year", 0);

		String uri = request.getServletPath();
		boolean viewLate = "/ViewLateJobOrders".equals(uri);
		String errorMsg;
		String reportTitle;

		if (viewLate) {
			errorMsg = "لا يوجد أوامر عمل متأخرة";
			reportTitle = Enums.REPORT_TITLE.LATE_JOB_ORDERS.getReportTitle();
		} else {
			errorMsg = "لا يوجد أوامر عمل مفتوحة";
			reportTitle = Enums.REPORT_TITLE.OPENED_JOB_ORDERS.getReportTitle();
		}

		List<JobOrder> jOrders = dbQueryManager.getJobOrders(siteId, locationId, month, year,
				(viewLate) ? Enums.JOB_ORDER_STATUS.LATE : Enums.JOB_ORDER_STATUS.OPENED);

		if (jOrders == null || jOrders.isEmpty()) {
			announceErorrMessage(request, response, errorMsg);
			return;
		}

		request.setAttribute("viewLate", viewLate);
		request.setAttribute("jobOrders", jOrders);
		viewHTMLReport(request, response, "report/ViewJobOrdersList.jsp", reportTitle);

	}
}
